<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\common\library\Email;
use app\common\model\Order as OrderModel;
use app\common\model\StoreProject;
use think\Db;

/**
 * 预约单
 *
 * @icon fa fa-circle-o
 */
class Order extends Backend
{

    /**
     * Order模型对象
     * @var \app\admin\model\Order
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Order;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    ->with(['store','project'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {
                $row->visible(['order_id','store_id','order_no','project_id','order_status','date','num','last_name','first_name',
                    'phone','email','remark','staff_id','cash_price','swiping_card_price','swiping_card_tip_price','gift_card_price',
                    'gift_card_tip_price','pay_status','pay_time','createtime','is_hang','is_confirm','is_cancel']);
                $row->visible(['store']);
				$row->getRelation('store')->visible(['store_name']);
				$row->visible(['project']);
				$row->getRelation('project')->visible(['project_name','price']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    //确认订单
    public function confirm($ids = null)
    {
        //$order_id = input('order_id',0);

        $info = OrderModel::get($ids);
        if (!$info)$this->error('订单不存在');
        if ($info->is_confirm == 1)$this->error('订单已确认');
        $store = \app\common\model\Store::get($info->store_id);
        $project = StoreProject::get($info->project_id);
        Db::startTrans();
        try {
            $info->is_confirm = 1;
            $info->confirm_time = time();
            $info->save();
            $message_title = '您预约'.$store['store_name'].'门店的项目:'.$project['project_name'].'已确认';
            $message_content = 'https://yuyueanmo.xing-peng.com';
            $email = new Email;
            $result = $email
                ->to($info->email)
                ->subject($message_title)
                ->message($message_content)
                ->send();

            if (!$result) {
                Db::rollback();
                $this->error($email->getError());
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('确认成功');
    }
}
